let ajaxTimes = 0
export const request = (params) => {
  // url中带有/my/ 请求头中携带token
  let header = {...params.header}
  if (params.url.includes('/my/')) {
    header['Authorization'] = wx.getStorageSync('token')
  }

  ajaxTimes ++
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
  // 定义公共路径
  const baseUrl = 'https://api-hmugo-web.itheima.net/api/public/v1'
  params.url = baseUrl + params.url
  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      header: header,
      success: (result) => {
        resolve(result)
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {
        ajaxTimes --
        if(ajaxTimes === 0) {
          wx.hideLoading()
        }
      }
    })
  })
}
